package com.chat.controller;

import com.chat.request.MessageGroupRequest;
import com.chat.request.SendMessageRequest;
import com.chat.request.WithdrawOneRequest;
import com.chat.response.BaseResponse;
import com.chat.service.PushService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cat/message")
public class PushController {
    @Resource
    private PushService pushService;


    /**
     * 推送给所有用户
     * @param msg
     */
    @PostMapping("/pushAll")
    public void pushToAll(@RequestParam("msg") String msg){
        pushService.pushMsgToAll(msg);
    }
    /**
     * 推送给指定用户
     */
    @PostMapping("/pushOne")
    public BaseResponse<String> pushMsgToOne(@RequestBody @Validated SendMessageRequest sendMessageDto) {
        Boolean ok = pushService.pushMsgToOne(sendMessageDto);
        if (ok) {
            return BaseResponse.success("联系成功");
        } else {
            return BaseResponse.success("联系方不在线，转离线消息");
        }
    }

    @PostMapping("/pushGroup")
    public BaseResponse<String> pushGroup(@RequestBody @Validated MessageGroupRequest messageGroupDTO) {
        try {
            pushService.pushMsgToGroup(messageGroupDTO);
            return BaseResponse.success("成功");
        } catch (Exception e) {
            return BaseResponse.failure(500, "错误");
        }
    }

    @PostMapping("/withdrawOne")
    public BaseResponse<String> withdraw(@RequestBody WithdrawOneRequest dto){
        try {
            pushService.WithdrawOne(dto);
            return BaseResponse.success("成功");
        } catch (Exception e) {
            return BaseResponse.failure(500, "错误");
        }
    }
}
